package com.xca.is.activity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.xca.is.adapter.WeatherWeekAdapter;
import com.xca.is.databinding.ActivityWeatherBinding;
import com.xca.is.entity.Weather;
import com.xca.is.http.HttpConnect;
import com.xca.is.model.WeatherModel;
import com.xca.is.utils.TimeUtils;
import com.xca.is.utils.WeatherBackgroundUtils;

import java.util.ArrayList;
import java.util.List;

public class WeatherActivity extends AppCompatActivity implements HttpConnect {
    private ActivityWeatherBinding binding ;
    private List<Weather.Result.Future> list ;
    private WeatherWeekAdapter  adapter ;
    private WeatherModel model ;
    private ViewModelProvider provider ;
    private MutableLiveData<Weather> liveData ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWeatherBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.wLoading.rLoading.setVisibility(View.GONE);

        list = new ArrayList<>() ;
        binding.wDateDayList.setLayoutManager(new GridLayoutManager(this,1));

        provider = new ViewModelProvider(this);
        model = provider.get(WeatherModel.class);
        model.setModel(this);
        liveData = (MutableLiveData<Weather>) model.getLiveData();
        liveData.observe(this, new Observer<Weather>() {
            @Override
            public void onChanged(Weather weather) {
                if (weather != null){
                    showData(weather);
                }
            }
        });
        adapter = new WeatherWeekAdapter(list,this,this);
        binding.wDateDayList.setAdapter(adapter);
        model.getData("吉林");
        onClickListener();
    }
    private void onClickListener(){
        binding.wSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String city = binding.wLocal.getText().toString().trim();
                if (city.isEmpty()){
                    binding.wLocal.setError("请输入城市名称");
                    return;
                }
                model.getData(city);
            }
        });
    }

    private void showData(Weather weather){
        if (weather != null){
            String weatherType = weather.getResult().getRealtime().getInfo();
            setWeatherBackground(weatherType);
            binding.wAqi.setText(weather.getResult().getRealtime().getAqi());
            binding.wWater.setText(weather.getResult().getRealtime().getHumidity());
            binding.wTemp.setText(weather.getResult().getRealtime().getTemperature());
            binding.wWeather.setText(weatherType);
            binding.wWind.setText(weather.getResult().getRealtime().getDirect() + " " + weather.getResult().getRealtime().getPower());
            binding.wTime.setText(TimeUtils.getTime()[0]);
            list = weather.getResult().getFuture();
            adapter.updateDate(list);
        }
    }
    private void setWeatherBackground(String weatherType) {
        String url = WeatherBackgroundUtils.getBackgroundUrl(weatherType);
        Log.d("获取到的图片路径", url);
        Glide.with(this)
                .asDrawable()
                .load(url)
                .centerCrop()//居中裁剪
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        binding.wa.setBackground(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }
    @Override
    public void Loading(boolean loading) {
        binding.wLoading.rLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void Success(Object object) {

    }

    @Override
    public void Fail(String... msg) {

    }

    @Override
    public void Error(String msg) {

    }

    @Override
    public void Complete() {
        binding.wLoading.rLoading.setVisibility(View.GONE);
    }

    @Override
    public void Timeout() {

    }

    @Override
    public void TokenError() {

    }
}
